<?php
	include('header.php');
?>		
		<!--[if !IE]>start content<![endif]-->					
		<div id="content">
			<div class="inner">
				
				<!--[if !IE]>start section<![endif]-->
		
				<div class="section">
					
					<!--[if !IE]>start title wrapper<![endif]-->
					<div class="title_wrapper">
						<span class="title_wrapper_top"></span>
						<div class="title_wrapper_inner">
							<span class="title_wrapper_middle"></span>
							<div class="title_wrapper_content">
								<h2><?php echo $lang['CONTENT_UPLOAD_FILE_EXCEL']; ?></h2>
							</div>
						</div>
						<span class="title_wrapper_bottom"></span>
					</div>
					<!--[if !IE]>end title wrapper<![endif]-->
					
					<!--[if !IE]>start section content<![endif]-->
					<div class="section_content">
						<span class="section_content_top"></span>
						
						<div class="section_content_inner">
							<?php
								
								
								
								if(isset($_POST['submitfile'])){ 
									
									if ($_FILES["file"]["type"] == "application/vnd.ms-excel" && 
										($_FILES["file"]["size"] < 100000000)){
											if ($_FILES["file"]["error"] > 0){
												echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
											}else{
												echo "Upload: " . $_FILES["file"]["name"] . "<br />";
												echo "Type: " . $_FILES["file"]["type"] . "<br />";
												//echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
												//echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

												//if (file_exists("upload/" . $_FILES["file"]["name"])){
												//	echo $_FILES["file"]["name"] . " already exists. ";
												//}else {
													move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
													echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
												//}
												
												$qUpload= "DESCRIBE upload";
												$rUpload = $db->sql_fetch($qUpload);
												foreach ($rUpload as $i) {
														//echo '<br />';
														//echo $i['Field'];
														$arrHeaderField[$i['Field']] = $i['Field'];
														//echo '<br />';
												}
												//var_dump($arrHeaderField);
												$data = new Spreadsheet_Excel_Reader();
												$data->setOutputEncoding('CP1251');
												$data->read("upload/" . $_FILES["file"]["name"]);   
												echo '<br />';
												$notValidHeader = false;
												for ($x = 1; $x <= count($data->sheets[0]["cells"]); $x++) {
													//$str=str_replace("'","''" ,$str);
													$twtdn_cfbrhtdn = str_replace("'","''" ,$data->sheets[0]["cells"][$x][1]);
													$twtdn_twtdnid = str_replace("'","''" ,$data->sheets[0]["cells"][$x][2]);
													$twtdn_twdocode = str_replace("'","''" ,$data->sheets[0]["cells"][$x][3]);
													$twtdn_arcustomerid = str_replace("'","''" ,$data->sheets[0]["cells"][$x][4]);
													$twtdn_arcustomername = str_replace("'","''" ,$data->sheets[0]["cells"][$x][5]);
													$twtdn_twrecname = str_replace("'","''" ,$data->sheets[0]["cells"][$x][6]);
													$twload_vmvehiclecode = str_replace("'","''" ,$data->sheets[0]["cells"][$x][7]);
													$twload_cfbrhload = str_replace("'","''" ,$data->sheets[0]["cells"][$x][8]);
													$twload_twloadid = str_replace("'","''" ,$data->sheets[0]["cells"][$x][9]);
													$twload_remark = str_replace("'","''" ,$data->sheets[0]["cells"][$x][10]);
													$twtdn_twqty = str_replace("'","''" ,$data->sheets[0]["cells"][$x][11]);
													$twtdn_twuom = str_replace("'","''" ,$data->sheets[0]["cells"][$x][12]);
													$twtdn_cfbrhtdncurrent = str_replace("'","''" ,$data->sheets[0]["cells"][$x][13]);
													$twtdn_twrecpostcode = str_replace("'","''" ,$data->sheets[0]["cells"][$x][14]);
													$twtdn_twrecstate = str_replace("'","''" ,$data->sheets[0]["cells"][$x][15]);
													$twtdn_twtdnstatus = str_replace("'","''" ,$data->sheets[0]["cells"][$x][16]);
													$compute_0017 = str_replace("'","''" ,$data->sheets[0]["cells"][$x][17]);
													$duration = str_replace("'","''" ,$data->sheets[0]["cells"][$x][18]);
													$recdatefrom = str_replace("'","''" ,$data->sheets[0]["cells"][$x][19]);
													$recdateto = str_replace("'","''" ,$data->sheets[0]["cells"][$x][20]);
													$twtdn_twremark = str_replace("'","''" ,$data->sheets[0]["cells"][$x][21]);
													$ardebtor_ardebtorcode = str_replace("'","''" ,$data->sheets[0]["cells"][$x][22]);
													$ardebtor_arshortname = str_replace("'","''" ,$data->sheets[0]["cells"][$x][23]);
													$aecase_cfbrhcase = str_replace("'","''" ,$data->sheets[0]["cells"][$x][24]);
													$aecase_aecaseid = str_replace("'","''" ,$data->sheets[0]["cells"][$x][25]);
													$aecase_aeerror = str_replace("'","''" ,$data->sheets[0]["cells"][$x][26]);
													$aecase_aeerrordesc = str_replace("'","''" ,$data->sheets[0]["cells"][$x][27]);
													$twtdn_twtdntype = str_replace("'","''" ,$data->sheets[0]["cells"][$x][28]);
													$twtdn_twcoveragetype = str_replace("'","''" ,$data->sheets[0]["cells"][$x][29]);
													$twload_cmcontractorcode = str_replace("'","''" ,$data->sheets[0]["cells"][$x][30]);
													$load_contractor_cmcontractorname = str_replace("'","''" ,$data->sheets[0]["cells"][$x][31]);
													
													if($x == 1) {
														if(!array_key_exists($data->sheets[0]["cells"][$x][1], $arrHeaderField)){  //1
															$errors[] = 'Header Name For '.$data->sheets[0]["cells"][$x][1].' Is Not Valid';
															$notValidHeader = true;
														}	
														if(!array_key_exists($data->sheets[0]["cells"][$x][2], $arrHeaderField)){  //2
															$errors[] = 'Header Name For '.$data->sheets[0]["cells"][$x][2].' Is Not Valid';
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][3], $arrHeaderField)){  //3
														    $errors[] = 'Header Name For '.$data->sheets[0]["cells"][$x][3].' Is Not Valid';
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][4], $arrHeaderField)){  //4
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][4]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][5], $arrHeaderField)){  //5
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][5]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][6], $arrHeaderField)){  //6
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][6]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][7], $arrHeaderField)){  //7
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][7]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][8], $arrHeaderField)){  //8
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][8]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][9], $arrHeaderField)){  //9
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][9]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][10], $arrHeaderField)){  //10
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][10]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][11], $arrHeaderField)){  //11
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][11]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][12], $arrHeaderField)){  //12
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][12]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][13], $arrHeaderField)){  //13
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][13]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][14], $arrHeaderField)){  //14
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][14]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][15], $arrHeaderField)){  //15
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][15]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][16], $arrHeaderField)){  //16
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][16]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][17], $arrHeaderField)){  //17
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][17]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][18], $arrHeaderField)){  //18
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][18]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][19], $arrHeaderField)){  //19
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][19]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][20], $arrHeaderField)){  //20
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][20]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][20], $arrHeaderField)){  //20
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][20]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][21], $arrHeaderField)){  //21
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][21]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][22], $arrHeaderField)){  //22
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][22]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][23], $arrHeaderField)){  //23
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][23]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][24], $arrHeaderField)){  //24
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][24]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][25], $arrHeaderField)){  //25
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][25]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][26], $arrHeaderField)){  //26
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][26]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][27], $arrHeaderField)){  //27
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][27]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][28], $arrHeaderField)){  //28
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][28]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][29], $arrHeaderField)){  //29
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][29]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][30], $arrHeaderField)){  //30
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][30]." Is Not Valid";
															$notValidHeader = true;
														}
														if(!array_key_exists($data->sheets[0]["cells"][$x][31], $arrHeaderField)){  //31
															$errors[] = "Header Name For ".$data->sheets[0]["cells"][$x][31]." Is Not Valid";
															$notValidHeader = true;
														}
															
														if($notValidHeader)
															break;
													}else{
														$uplCheck= "select COUNT(*) from upload where twtdn_cfbrhtdn = '$twtdn_cfbrhtdn' and twtdn_twtdnid='$twtdn_twtdnid' ";
														$uplRes = $db->sql_total($uplCheck);
														
														if($uplRes == 0){
															//Insert data Upload Table
															$sqlInsertUpload = "INSERT INTO upload 
																(created_date,created_by,
																	twtdn_cfbrhtdn,twtdn_twtdnid,twtdn_twdocode,twtdn_arcustomerid,twtdn_arcustomername,twtdn_twrecname,
																	twload_vmvehiclecode,twload_cfbrhload,twload_twloadid,twload_remark,
																	twtdn_twqty,twtdn_twuom,twtdn_cfbrhtdncurrent,twtdn_twrecpostcode,twtdn_twrecstate,twtdn_twtdnstatus,
																	compute_0017,duration,recdatefrom,recdateto,twtdn_twremark,ardebtor_ardebtorcode,ardebtor_arshortname,
																	aecase_cfbrhcase,aecase_aecaseid,aecase_aeerror,aecase_aeerrordesc,
																	twtdn_twtdntype,twtdn_twcoveragetype,twload_cmcontractorcode,load_contractor_cmcontractorname) 
																VALUES (NOW(),'$_SESSION[log_userlogin_id]',
																	'$twtdn_cfbrhtdn',$twtdn_twtdnid,'$twtdn_twdocode','$twtdn_arcustomerid','$twtdn_arcustomername','$twtdn_twrecname',
																	'$twload_vmvehiclecode','$twload_cfbrhload','$twload_twloadid','$twload_remark',
																	'$twtdn_twqty','$twtdn_twuom','$twtdn_cfbrhtdncurrent','$twtdn_twrecpostcode','$twtdn_twrecstate','$twtdn_twtdnstatus',
																	'$compute_0017','$duration','$recdatefrom','$recdateto','$twtdn_twremark','$ardebtor_ardebtorcode','$ardebtor_arshortname',
																	'$aecase_cfbrhcase','$aecase_aecaseid','$aecase_aeerror','$aecase_aeerrordesc',
																	'$twtdn_twtdntype','$twtdn_twcoveragetype','$twload_cmcontractorcode','$load_contractor_cmcontractorname')";
															$resInsertUpload = $db->sql_query($sqlInsertUpload);
															$uploadID = $db->sql_insertID();
															
															//Get Postcode Info
															$poscodeSql= "select * from lookup_postcode where postcode = '$twtdn_twrecpostcode'";
															$posGet = $db->sql_list($poscodeSql);
															$postcode = $posGet['postcode'];
															$city = $posGet['post_office'];
															$statecode = $posGet['state_code'];
															
																
															//Check data Sender
															$entCheck= "select COUNT(*) from entity where ent_code = '$ardebtor_ardebtorcode'";
															$entRes = $db->sql_total($entCheck);
															
															//IF Sender Existed
															if($entRes > 0){
																
																//Get Sender
																$entSenderSql= "select * from entity where ent_code = '$ardebtor_ardebtorcode'";
																$entSenderGet = $db->sql_list($entSenderSql);
																$senderId = $entSenderGet['ent_id'];
																
																//Check Receiver
																$entReceiverCheck= "select COUNT(*) from entity where ent_short_name = '$twtdn_twrecname'";
																$entReceiverRes= $db->sql_total($entReceiverCheck);
																$receiverId = 0;
																
																
																
																//Get Receiver Info Or Add
																if($entReceiverRes > 0){
																	$entReceiverSql= "select * from entity where ent_short_name = '$twtdn_twrecname'";
																	$entReceiverGet = $db->sql_list($entReceiverSql);
																	$receiverId = $entReceiverGet['ent_id'];
																}else{
																	$sqlInsertReceiverEnt = "INSERT INTO entity 
																					(ent_name,ent_short_name,
																						ent_add_postcode,ent_add_city,ent_add_state,
																						ent_created_date,ent_created_by) 
																					VALUES
																					('$twtdn_twrecname','$twtdn_twrecname',
																					 '$twtdn_twrecpostcode','$city','$statecode',
																					 NOW(),'$_SESSION[log_userlogin_id]')";
																	$resInsertReceiverEnt = $db->sql_query($sqlInsertReceiverEnt);
																	$receiverId = $db->sql_insertID();
																}
															}
															//IF Sender Not Existed
															else{
																//Add Sender Info
																 $sqlInsertSenderEnt = "INSERT INTO entity 
																					(ent_name,ent_short_name,ent_old_id,ent_code,
																						ent_created_date,ent_created_by) 
																					VALUES
																					('$twtdn_arcustomername','$ardebtor_arshortname','$twtdn_arcustomerid','$ardebtor_ardebtorcode',
																					 NOW(),'$_SESSION[log_userlogin_id]')";
																$resInsertSenderEnt = $db->sql_query($sqlInsertSenderEnt);
																$senderId = $db->sql_insertID();
															
																//Check Receiver
																$entReceiverCheck= "select COUNT(*) from entity where ent_short_name = '$twtdn_twrecname'";
																$entReceiverRes= $db->sql_total($entReceiverCheck);
																$receiverId = 0;
																
																
																//Get Receiver Info Or Add
																if($entReceiverRes > 0){
																	$entReceiverSql= "select * from entity where ent_short_name = '$twtdn_twrecname'";
																	$entReceiverGet = $db->sql_list($entReceiverSql);
																	$receiverId = $entReceiverGet['ent_id'];
																}else{
																	$sqlInsertReceiverEnt = "INSERT INTO entity 
																					(ent_name,ent_short_name,
																						ent_add_postcode,ent_add_city,ent_add_state,
																						ent_created_date,ent_created_by) 
																					VALUES
																					('$twtdn_twrecname','$twtdn_twrecname',
																					 '$twtdn_twrecpostcode','$city','$statecode',
																					 NOW(),'$_SESSION[log_userlogin_id]')";
																	$resInsertReceiverEnt = $db->sql_query($sqlInsertReceiverEnt);
																	$receiverId = $db->sql_insertID();
																}
															}
															//Insert into delivering info
															
															if($uploadID > 0 && $senderId > 0 && $receiverId > 0) {
																$tpd_status="TPD00";
																$tpd_status_remarks="Upload Data From HQ";
																$tpd_tdn_no = $twtdn_cfbrhtdn.$twtdn_twtdnid;
																$sqlInsertTPD = "  INSERT INTO transport_package_delivering 
																				(tpd_sender_ent_id,tpd_receiver_ent_id,tpd_upload_id,tpd_tdn_no,tpd_do_ref,
																					tpd_qty,tpd_qty_type,tpd_type_pallet,
																					tpd_status,tpd_status_remarks,tpd_created_date,tpd_created_by) 
																				VALUES
																				('$senderId','$receiverId','$uploadID','$tpd_tdn_no','$twtdn_twdocode',
																					'$twtdn_twqty','$twtdn_twuom','$twtdn_twtdntype',
																					'$tpd_status','$tpd_status_remarks',now(),'$_SESSION[log_userlogin_id]')";
																$resInsertTPD = $db->sql_query($sqlInsertTPD);
																$TPDid = $db->sql_insertID();
																
																$sqlInsertLog = " INSERT INTO tpd_status_log 
																			(tsl_tpd_id,tsl_tdn_no,tsl_status,tsl_status_remarks,tsl_created_date,tsl_created_by) 
																				VALUES
																			('$TPDid','$tpd_tdn_no','$tpd_status','$tpd_status_remarks',NOW(),'$_SESSION[log_userlogin_id]')";
																$resInsertLog= $db->sql_query($sqlInsertLog);
																
															}
														}else {
															//update info upload here
														}
													
														
													}
												}
												
												if($notValidHeader == true){
													echo"
														<div class='section'>
															<ul class='system_messages'>
															<li class='yellow'><span class='ico'></span>
																<strong class='system_title'>Warning! Checking Data In Excel</strong>";
													while (list($key,$value) = each($errors))
													{
															echo "<br/><span class='system_title'>".$value."</span>";
													}
													echo "	</li>
															</ul>
														</div>";
												}
											}
									}else{
										echo "Invalid file";
									}
									
									
									
								  
								}
								
							?>
							<form action="upload.php" method="POST" class="search_form" enctype="multipart/form-data" >
							
							
							<!--[if !IE]>start fieldset<![endif]-->
							<fieldset>
								<!--[if !IE]>start forms<![endif]-->
								<div class="forms">
								<h3><?php echo $lang['CONTENT_UPLOAD_FILE_EXCEL_ONLY']; ?>  </h3>
								<!--[if !IE]>start row<![endif]-->
								<div class="row">
									<label><?php echo $lang['CONTENT_UPLOAD_CHOOSE_FILE']; ?>:</label>
									<div class="inputs">
										<span class="input_wrapper" style="width:430px;">
											<input type="hidden" name="MAX_FILE_SIZE" value="100000000"> 
											<input class="text" size="55" type="file" name="file" value="AttachFile"  accept="text/*" tabindex=18 >
											
										</span>
									</div>
								</div>
								<!--[if !IE]>end row<![endif]-->
								
								<!--[if !IE]>start row<![endif]-->
								<div class="row">
									<div class="inputs">
										<span class="button blue_button search_button"><span><span><em><?php echo $lang['CONTENT_UPLOAD']; ?></em></span></span><input name="submitfile" type="submit" /></span>
									</div>
								</div>
								<!--[if !IE]>end row<![endif]-->
								
								</div>
								<!--[if !IE]>end forms<![endif]-->
								
							</fieldset>
							<!--[if !IE]>end fieldset<![endif]-->
							
							
							</form>
							<br />
							<?php
							
							/* 
									   First get total number of rows in data table. 
									   If you have a WHERE clause in your query, make sure you mirror it here.
									*/
									$date = date("Y-m-d"); 
									$qCount= "select COUNT(*) from upload where DATE_FORMAT(created_date, '%Y-%m-%d') = '$date' and  created_by ='$_SESSION[log_userlogin_id]'
									order by created_date ";
									$total_pages = $db->sql_total($qCount);
									
									if($total_pages > 0){
										$tarikh = new DateTime();
										$tarikh = $tarikh->format('d-m-Y');
										echo '<h4>'.$lang['CONTENT_TODAY'].' : '.$tarikh.'  </h4>';
										echo '<h4>'.$lang['CONTENT_TOTAL_UPLOAD'].' : '.$total_pages.' '.$lang['CONTENT_RECORDS'].'</h4>';
									}
									/* Setup vars for query. */
									$limit = 10; 								//how many items to show per page
							?>
							<!--[if !IE]>start table_wrapper<![endif]-->
							<div class="table_wrapper">
								<div class="table_wrapper_inner">
								<?php  
									$page="";
									if(isset($_GET['page']))
										$page=$_GET['page'];
									
									if($page) 
										$start = ($page - 1) * $limit; 			//first item to display on this page
									else
										$start = 0;								//if no page var is given, set start to 0
									
									/* Get data. */
									$qList= "select * from  upload where DATE_FORMAT(created_date, '%Y-%m-%d') = '$date' and  created_by ='$_SESSION[log_userlogin_id]'
												order by created_date 
												 LIMIT $start, $limit ";
									$rList = $db->sql_fetch($qList);			

								if($total_pages > 0){	
								?>
								<table cellpadding="0" cellspacing="0" width="100%">
									<tbody>
									<tr>
										<th  style="width: 10px;">Bil.</th>
										<th style="width: 50px;">twtdn_cfbrhtdn</th>
										<th style="width: 50px;">twtdn_twtdnid</th>
										<th  style="width: 50px;">twtdn_twdocode</th>
										<th>twtdn_arcustomername</th>
										<th>twtdn_twrecname</th>
									</tr>
								<?php
								}
									$num = $start;
									foreach ($rList as $i) {
										$num++;
										$colorRow = "first";
										if($num%2==0)
											$colorRow = "second"; 
										else
											$colorRow = "first";
											
										echo '	
											<tr class="'.$colorRow.'">
												<td>'.$num.'</td>
												<td>'.$i['twtdn_cfbrhtdn'].'</td>
												<td>'.$i['twtdn_twtdnid'].'</td>
												<td>'.$i['twtdn_twdocode'].'</td>
												<td>'.$i['twtdn_arcustomername'].'</td>
												<td>'.$i['twtdn_twrecname'].'</td>
											</tr>';
										
									}
								?>
								</tbody></table>
								</div>
							</div>
							<!--[if !IE]>end table_wrapper<![endif]-->
							
						</div>
						
						<!--[if !IE]>start pagination<![endif]-->
							<div class="pagination_wrapper">
							<span class="pagination_top"></span>
							<div class="pagination_middle">
							<div class="pagination">
								<?php
								// How many adjacent pages should be shown on each side?
									$adjacents = 3;
								/* Setup page vars for display. */
									if ($page == 0) $page = 1;					//if no page var is given, default to 1.
									$prev = $page - 1;							//previous page is page - 1
									$next = $page + 1;							//next page is page + 1
									$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
									$lpm1 = $lastpage - 1;						//last page minus 1
								/* 
									Now we apply our rules and draw the pagination object. 
									We're actually saving the code to a variable in case we want to draw it more than once.
								*/
								echo '<span class="page_no">'.$lang['CONTENT_PAGINATION_PAGE'].' '.$page.' of '.$lastpage.' </span>';
								echo '<ul class="pag_list">';

									//previous button
									if ($page > 1) 
										echo '<li><a href="upload.php?page='.$prev.'" class="pag_nav"><span><span>'.$lang['CONTENT_PAGINATION_PREVIOUS'].'</span></span></a> </li>';
									else
										echo '<li><a href="#" class="pag_nav"><span><span>'.$lang['CONTENT_PAGINATION_PREVIOUS'].'</span></span></a> </li>';

									//pages	
									if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
									{	
										for ($counter = 1; $counter <= $lastpage; $counter++)
										{
											if ($counter == $page)
												echo '<li><a href="#" class="current_page"><span><span>'.$counter.'</span></span></a></li>';
											else	
												echo '<li><a href="upload.php?page='.$counter.'">'.$counter.'</a></li>';	
										}
									}
									elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
									{
										//close to beginning; only hide later pages
										if($page < 1 + ($adjacents * 2))		
										{
											for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
											{
												if ($counter == $page)
													echo '<li><a href="#" class="current_page"><span><span>'.$counter.'</span></span></a></li>';
												else	
													echo '<li><a href="upload.php?page='.$counter.'">'.$counter.'</a></li>';													
											}
											echo '<li>[...]</li>';
											echo '<li><a href="upload.php?page='.$lpm1.'">'.$lpm1.'</a></li>';	
											echo '<li><a href="upload.php?page='.$lastpage.'">'.$lastpage.'</a></li>';		
										}
										//in middle; hide some front and some back
										elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
										{
											echo '<li><a href="upload.php?page=1'.'">1</a></li>';	
											echo '<li><a href="upload.php?page=2'.'">2</a></li>';
											echo '<li>[...]</li>';											
											for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
											{
												if ($counter == $page)
													echo '<li><a href="#" class="current_page"><span><span>'.$counter.'</span></span></a></li>';
												else
													echo '<li><a href="upload.php?page='.$counter.'">'.$counter.'</a></li>';						
											}
											echo '<li>[...]</li>';
											echo '<li><a href="upload.php?page='.$lpm1.'">'.$lpm1.'</a></li>';	
											echo '<li><a href="upload.php?page='.$lastpage.'">'.$lastpage.'</a></li>';												
										}
										//close to end; only hide early pages
										else
										{
											echo '<li><a href="upload.php?page=1'.'">1</a></li>';	
											echo '<li><a href="upload.php?page=2'.'">2</a></li>';
											echo '<li>[...]</li>';	
											for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
											{
												if ($counter == $page)
													echo '<li><a href="#" class="current_page"><span><span>'.$counter.'</span></span></a></li>';
												else
													echo '<li><a href="upload.php?page='.$counter.'">'.$counter.'</a></li>';						
											}
										}
									}
									//next button
									if ($page < $counter - 1) 
										echo '<li><a href="upload.php?page='.$next.'" class="pag_nav"><span><span>'.$lang['CONTENT_PAGINATION_NEXT'].'</span></span></a> </li>';
									else
										echo '<li><a href="#" class="pag_nav"><span><span>'.$lang['CONTENT_PAGINATION_NEXT'].'</span></span></a> </li>';
									?>
								</ul>
							</div>
							</div>
							<span class="pagination_bottom"></span>
							</div>
						<!--[if !IE]>end pagination<![endif]-->
						<span class="section_content_bottom"></span>
					</div>
					<!--[if !IE]>end section content<![endif]-->
				</div>
				<!--[if !IE]>end section<![endif]-->
				
					
				
			</div>
		</div>
		<!--[if !IE]>end content<![endif]-->

<?php
	include('footer.php');
?>